package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {

    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    /**
     * 套餐列表
     *
     * @return
     */
    @Override
    public List<Setmeal> list(Setmeal setmeal) {
        return setmealMapper.list(setmeal);
    }

    /**
     * 根据套餐id查询套餐详情
     * @param setmealId
     * @return
     */
    @Override
    public List<DishItemVO> getDishItemBySetmealId(Long setmealId) {
        return setmealMapper.getDishItemBySetmealId(setmealId);
    }

    /**
     * 新增套餐
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void saveSetmealWithDish(SetmealDTO setmealDTO) {
        // 1. 新增套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);

        // 2. 新增套餐菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        // 1. 开启分页功能
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        // 2. 执行分页查询
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);

        // 3. 封装分页结果
        return new PageResult(page.getTotal(),page.getResult());
    }

    /**
     * 套餐状态修改
     * @param status
     * @param id
     */
    @Override
    public void startOrStopSetmeal(Integer status, Long id) {
        // 1. 构建 Setmeal 实体对象
        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();

        // 2. 更新套餐状态
        setmealMapper.update(setmeal);
    }

    /**
     * 套餐删除
     * @param ids
     */
    @Override
    @Transactional
    public void deleteBatchByIds(List<Long> ids) {
        // 1. 删除套餐菜品关系
        setmealDishMapper.deleteBySetmealIds(ids);

        // 2. 删除套餐
        setmealMapper.deleteByIds(ids);
    }

    /**
     * 套餐修改
     * @param setmealDTO
     */
    @Override
    @Transactional
    public void updateSetmealWithDish(SetmealDTO setmealDTO) {
        // 1. 构建 Setmeal 实体对象
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);

        // 2. 删除套餐菜品关系
        setmealDishMapper.deleteBySetmealId(setmeal.getId());

        // 3. 新增套餐菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmeal.getId());
        });
        setmealDishMapper.insertBatch(setmealDishes);

        // 4. 更新数据
        setmealMapper.update(setmeal);
    }

    /**
     * 根据套餐id查询套餐详情
     * @param id
     * @return
     */
    @Override
    public SetmealVO getById(Long id) {
        // 1. 查询套餐
        Setmeal setmeal = setmealMapper.selectById(id);

        // 2. 查询套餐菜品
        List<SetmealDish> setmealDishes = setmealDishMapper.selectBySetmealId(id);

        // 3. 封装 SetmealVO 对象
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        setmealVO.setSetmealDishes(setmealDishes);

        // 4. 返回数据
        return setmealVO;
    }
}
